<template>
  <el-card class="main-card">
    <div class="title">{{ this.$route.name }}</div>
    <!-- 表格操作 -->
    <div class="operation-container">
      <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="openModel(null)"
      >
        新增
      </el-button>
      <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          :disabled="schoolIdList.length == 0"
          @click="openDelete = true"
      >
        批量删除
      </el-button>
      <!-- 条件筛选 -->
      <div style="margin-left:auto">
        <!-- 院校名 -->
        <el-input
            clearable
            v-model="keywords"
            prefix-icon="el-icon-search"
            size="small"
            placeholder="请输入院校名"
            style="width:200px"
            @keyup.enter.native="searchSchool"
        />
        <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            style="margin-left:1rem"
            @click="searchSchool"
        >
          搜索
        </el-button>
      </div>
    </div>
    <!-- 表格展示 -->
    <el-table
        border
        :data="schoolList"
        @selection-change="selectionChange"
        v-loading="loading"
    >
      <!-- 表格列 -->
      <el-table-column type="selection" width="55"/>
      <el-table-column
          prop="img"
          label="院校图片"
          width="180"
          align="center"
      >
        <template slot-scope="scope">
          <el-image
              class="school-cover"
              :src="
              scope.row.img
                ? scope.row.img
                : 'https://static.talkxj.com/articles/c5cc2b2561bd0e3060a500198a4ad37d.png'
            "
          />
          <i class="iconfont el-icon-mygongkai movie-status-icon"/>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名字" align="center"/>
      <el-table-column prop="province" label="省份" width="110" align="center"/>
      <el-table-column prop="major" label="专业" width="110" align="center"/>
      <el-table-column prop="address" label="详细地址" width="110" align="center"/>
      <el-table-column prop="content" label="简介" width="110" align="center"/>
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="130"
          align="center"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" style="margin-right:5px"/>
          {{ scope.row.createTime | date }}
        </template>
      </el-table-column>
      <!-- 列操作 -->
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
              type="primary"
              size="mini"
              @click="openModel(scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm
              title="确定删除吗？"
              style="margin-left:10px"
              @confirm="deleteSchool(scope.row.id)"
          >
            <el-button size="mini" type="danger" slot="reference">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        class="pagination-container"
        background
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="current"
        :page-size="size"
        :total="count"
        :page-sizes="[10, 20]"
        layout="total, sizes, prev, pager, next, jumper"
    />
    <!-- 批量逻辑删除对话框 -->
    <el-dialog :visible.sync="openDelete" width="30%">
      <div class="dialog-title-container" slot="title">
        <i class="el-icon-warning" style="color:#ff9900"/>提示
      </div>
      <div style="font-size:1rem">是否删除选中项？</div>
      <div slot="footer">
        <el-button @click="openDelete = false">取 消</el-button>
        <el-button type="primary" @click="deleteSchool(null)">
          确 定
        </el-button>
      </div>
    </el-dialog>
    <!-- 添加编辑对话框 -->
    <el-dialog :visible.sync="addOrEdit" width="30%">
      <div class="dialog-title-container" slot="title" ref="schoolTitle"/>
      <el-form label-width="80px" size="medium" :model="schoolForm">
        <el-form-item label="院校名">
          <el-input placeholder="请输入院校名" v-model="schoolForm.name" style="width:220px"/>
        </el-form-item>
        <el-form-item label="上传图片">
          <el-upload
              class="upload-cover"
              drag
              action="/api/admin/articles/images"
              multiple
              :before-upload="beforeUpload"
              :on-success="uploadCover"
          >
            <i class="el-icon-upload" v-if="schoolForm.img == ''"/>
            <div class="el-upload__text" v-if="schoolForm.img == ''">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <img v-else :src="schoolForm.img" width="360px" height="180px"/>
          </el-upload>
        </el-form-item>
        <el-form-item label="所在省份">
          <el-input placeholder="请输入" v-model="schoolForm.province" style="width:220px"/>
        </el-form-item>
        <el-form-item label="专业">
          <el-input placeholder="请输入" v-model="schoolForm.major" style="width:220px"/>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入详细地址"
              v-model="schoolForm.address">
          </el-input>
        </el-form-item>
        <el-form-item label="院校简介">
          <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入简介"
              v-model="schoolForm.content">
          </el-input>
        </el-form-item>
        <el-form-item label="官网链接">
          <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入"
              v-model="schoolForm.url">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addOrEdit = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditschool">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import * as imageConversion from "image-conversion";

export default {
  created() {
    this.listSchool();
    this.listCategories();
  },
  data: function () {
    return {
      loading: true,
      addOrEdit: false,
      schoolList: [],
      schoolIdList: [],
      categoryList: [],
      categoryName: "",
      keywords: null,
      categoryId: null,
      openDelete: false,
      isDelete: 0,
      current: 1,
      size: 10,
      count: 0,
      schoolForm: {
        id: null,
        name: null,
        img: "",
        viewsCount: 0,
        categoryName: ""
      },
      modeList: [{code: '1', name: "是"}, {code: '0', name: "否"}],
    };
  },
  methods: {
    selectionChange(schoolList) {
      this.schoolIdList = [];
      schoolList.forEach(item => {
        this.schoolIdList.push(item.id);
      });
    },
    searchSchool() {
      this.current = 1;
      this.listSchool();
    },
    openModel(school) {
      this.schoolForm = {img: '', categoryName: "", viewsCount: 0,}
      if (school != null) {
        this.schoolForm = JSON.parse(JSON.stringify(school));
        this.$refs.schoolTitle.innerHTML = "修改院校";
      } else {
        this.schoolForm.id = null;
        this.schoolForm.categoryName = "";
        this.$refs.schoolTitle.innerHTML = "添加院校";
      }
      this.addOrEdit = true;
    },
    deleteSchool(id) {
      var param = {};
      if (id == null) {
        param = {data: this.schoolIdList};
      } else {
        param = {data: [id]};
      }
      this.axios.delete("/api/admin/school", param).then(({data}) => {
        if (data.flag) {
          this.$notify.success({
            title: "成功",
            message: data.message
          });
          this.listSchool();
        } else {
          this.$notify.error({
            title: "失败",
            message: data.message
          });
        }
        this.openDelete = false;
      });
    },
    sizeChange(size) {
      this.size = size;
      this.listSchool();
    },
    currentChange(current) {
      this.current = current;
      this.listSchool();
    },
    listSchool() {
      this.axios
          .get("/api/school/list", {
            params: {
              current: this.current,
              size: this.size,
              keywords: this.keywords,
              categoryId: this.categoryId,
              tagId: this.tagId,
              isDelete: this.isDelete,
            }
          })
          .then(({data}) => {
            this.schoolList = data.data.recordList;
            this.count = data.data.count;
            this.loading = false;
          });
    },
    listCategories() {
      this.axios.get("/api/admin/categories/search", {
        params: {type: "1"}
      }).then(({data}) => {
        this.categoryList = data.data;
      });
    },
    addOrEditschool() {
      if (this.schoolForm.name.trim() == "") {
        this.$message.error("院校名不能为空");
        return false;
      }
      this.axios
          .post("/api/admin/school", this.schoolForm)
          .then(({data}) => {
            if (data.flag) {
              this.$notify.success({
                title: "成功",
                message: data.message
              });
              this.listSchool();
            } else {
              this.$notify.error({
                title: "失败",
                message: data.message
              });
            }
            this.addOrEdit = false;
          });
    },
    searchCategories(keywords, cb) {
      this.axios
          .get("/api/admin/categories/search", {
            params: {
              keywords: keywords,
              type: "1"
            }
          })
          .then(({data}) => {
            cb(data.data);
          });
    },
    handleSelectCategories(item) {
      this.addCategory({
        categoryName: item.categoryName,
      });
    },
    saveCategory() {
      if (this.categoryName.trim() != "") {
        this.addCategory({
          categoryName: this.categoryName
        });
        this.categoryName = "";
      }
    },
    addCategory(item) {
      this.schoolForm.categoryName = item.categoryName;
    },
    removeCategory() {
      this.schoolForm.categoryName = null;
    },
    uploadCover(response) {
      this.schoolForm.img = response.data;
    },
    beforeUpload(file) {
      return new Promise(resolve => {
        if (file.size / 1024 < this.config.UPLOAD_SIZE) {
          resolve(file);
        }
        // 压缩到200KB,这里的200就是要压缩的大小,可自定义
        imageConversion
            .compressAccurately(file, this.config.UPLOAD_SIZE)
            .then(res => {
              resolve(res);
            });
      });
    },
  },
  watch: {
    categoryId() {
      this.current = 1;
      this.listS();
    },
    openDelete() {
      this.current = 1;
      this.listSchool();
    }
  }
};
</script>

<style scoped>
.operation-container {
  margin-top: 1.5rem;
}

.school-status-menu span {
  margin-right: 24px;
}

.school-cover {
  position: relative;
  width: 100%;
  height: 90px;
  border-radius: 4px;
}

.school-cover::after {
  content: "";
  background: rgba(0, 0, 0, 0.3);
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.school-status-icon {
  color: #fff;
  font-size: 1.5rem;
  position: absolute;
  right: 1rem;
  bottom: 1.4rem;
}

.popover-title {
  margin-bottom: 1rem;
  text-align: center;
}

.popover-container {
  margin-top: 1rem;
  height: 260px;
  overflow-y: auto;
}

.category-item {
  cursor: pointer;
  padding: 0.6rem 0.5rem;
}

.category-item:hover {
  background-color: #f0f9eb;
  color: #67c23a;
}

</style>
